const express = require('express');
const router = express.Router();
var child_process = require('child_process');
var shellObj = require('../shell');
const restify = require('express-restify-mongoose');
//
// const Subscriber = require('../models/subscriber');
// restify.serve(router, Subscriber, {
//     prefix: '',
//     version: '',
//     idProperty: 'imsi'
// });

const Profile = require('../models/profile');
restify.serve(router, Profile, {
    prefix: '',
    version: ''
});

const Account = require('../models/account');
restify.serve(router, Account, {
    prefix: '',
    version: '',
    idProperty: 'username'
});


const Equipmentinfo = require('../models/equipmentinfo');
restify.serve(router, Equipmentinfo, {
    prefix: '',
    version: ''
});

const Amf = require('../models/amf');
restify.serve(router, Amf, {
    prefix: '',
    version: ''
});

const Smf = require('../models/smf');
restify.serve(router, Smf, {
    prefix: '',
    version: ''
});

const Dp = require('../models/dp');
restify.serve(router, Dp, {
    prefix: '',
    version: ''
});


router.get('/shellAction', (req, res) => {
    var getcmd = shellObj.shellString;
    var cmd=getcmd.replace(/SSSSS/g,req.query.time)
    console.log(shellObj.name + ': ' + cmd);
    child_process.exec(cmd, function (error, stdout, stderr) {
        const result = {stdout, stderr, error};
        console.log('|-------------------------------------shell run-------------------------------------|');
        console.log(result);
        console.log('|-------------------------------------shell run END-------------------------------------|');
        return res.json('执行成功！')
    });
});

router.get('/shellActionStop', (req, res) => {
    var cmd = shellObj.stopShell;
    console.log(shellObj.stopName + ': ' + cmd);
    child_process.exec(cmd, function (error, stdout, stderr) {
        const result = {stdout, stderr, error};
        console.log('|-------------------------------------shell run-------------------------------------|');
        console.log(result);
        console.log('|-------------------------------------shell run END-------------------------------------|');
        return res.json('执行成功！')
    });
});


module.exports = router;